<template>
    <div class="box2">
        <div class="inpBox">
			<el-input class="inp" v-model="inpstuName" :value="inpstuName" @keyup.enter.native="search"  placeholder="请输入姓名"></el-input>
			<el-input class="inp" v-model="inpstuNum" :value="inpstuNum" @keyup.enter.native="search" placeholder="请输入学号"></el-input>
			<el-button class="btn" style="background: #106ab1; color: #fff;" @click="search" icon="el-icon-search">搜索</el-button>
            <div class="btnBox">
                <el-button type="success" @click="addStudent" :disabled="submitFlag">完成</el-button>
                <h3 class="span" @click="$router.push('/classmanager/classdetails?clazzId='+$route.query.clazzId+'&clazzName='+$route.query.clazzName)">返回</h3>
            </div>
			
		</div>
        <el-table class="tab" :data="studentInfo" @selection-change="getStuId" :row-key="getRowKey">
			<el-table-column type="selection" :reserve-selection="true" width="100"></el-table-column>
			<el-table-column label="姓名" prop="stuName"></el-table-column>
			<el-table-column label="学号" prop="stuNumber"></el-table-column>
			<el-table-column label="性别" prop="stuSex"></el-table-column>
			<el-table-column label="联系方式" prop="stuTel"></el-table-column>
            <el-table-column label="年龄" prop="stuAge"></el-table-column>
		</el-table>
       
            <el-pagination
                class="page"
                layout="prev, pager, next"
                :total="totalSize"
				:current-page="pageIndex"
				@current-change="changePage"
				:page-size="pageSize">
            </el-pagination>
      
    </div>

</template>

<script>
export default {
    data(){
        return{
            baseUrl:require("../js/baseUrl.js").baseUrl,
            checked: false,
            inpstuName:'',
            inpstuNum:'',
            studentInfo:[],
            searchWord1:'',
            searchWord2:'',
            pageIndex:1,//当前显示的页
            pageSize:5,//每页显示的条数
            totalSize:0,//总数,
            studentId:[],
            realStudentId:[],
            submitFlag:true,
        }
    },
    methods:{
        getRowKey(row){
            return row.stuId;
        },
        //获取学生:SchoolSys/stu/like
        getStudent(){
            this.$http.post(this.baseUrl+'SchoolSys/stu/like',{
                pageIndex:this.pageIndex,
                pageSize:this.pageSize
            }).then(res=>{
                // console.log(res.data.data);
                if(res.data.code==200){
                    this.studentInfo=res.data.data.data
                    this.totalSize=res.data.data.a
                }
                
            })
        },
        //获取学生id
        getStuId(StuId){
            // console.log(StuId);

            let arr=[]
            StuId.map((v) => {
                arr.push(v.stuId)
            })
            this.studentId=arr
            // console.log(this.studentId);

            const countMap = new Map();
            let realList=[]
            this.studentId.forEach(v=>{
                if(countMap.has(v)){
                countMap.set(v, countMap.get(v) + 1);
                }else{
                countMap.set(v, 1);
                }
            })
            // console.log(countMap)
            countMap.forEach((count, num) => {
                if(count%2==0){
                
                }else{
                realList.push(num)
                }
            });
            this.realStudentId=realList
            console.log(this.realStudentId);
            if(this.realStudentId!=''){
                this.submitFlag=false
            }else{
                this.submitFlag=true
            }
            
        },
        //向该班级中添加学生:SchoolSys/clazz/shiftsadd
        addStudent(){
            this.$http.post(this.baseUrl+'SchoolSys/clazz/shiftsadd',{
                clazzId:this.$route.query.clazzId,
                liststuId:this.realStudentId
            }).then(res=>{
                // console.log(res);
                if(res.data.code==200){
                    this.$router.push('/classmanager/classdetails?clazzId='+this.$route.query.clazzId+'&clazzName='+this.$route.query.clazzName)
                    this.$message.success('学生添加成功')
                }else{
                    this.$message.error('学生添加失败')
                }
            })
            this.centerDialogVisible = false
           
        },
        //分页
        changePage(page){
            this.pageIndex=page;
            if(this.searchWord1!=""){
                this.searchStudent()
            }else if(this.searchWord2!=""){
                this.searchStudent()
            }else{
                this.getStudent()
            }
		},
        //搜索
        searchStudent(){
            this.$http.post(this.baseUrl+'SchoolSys/stu/like',{
                stuName:this.searchWord1,
                stuNum:this.searchWord2,
                pageIndex:this.pageIndex,
                pageSize:this.pageSize
            }).then(res=>{
                // console.log(res);
                if(res.data.code==200){
                    this.studentInfo=res.data.data.data
                    this.totalSize=res.data.data.a
                }else{
                    this.studentInfo=''
                    this.totalSize=1
                }
            })
        },
        search(){
            if(this.inpstuName==""){
                this.searchWord1=null
            }else{
                this.searchWord1=this.inpstuName
            }
            if(this.inpstuNum==""){
                this.searchWord2=null
            }else{
                this.searchWord2=this.inpstuNum
            }
            this.searchStudent()
        },
    },
    mounted(){
        this.getStudent()
    }
}
</script>


<style lang="less">
    .box2{
        position: relative;
        .inpBox{
        .span{
            margin-left: 20px;
            cursor: pointer;
            display: inline-block;
        }
        }
        .addstudent .top{
            display: flex;
            justify-content: space-between;
            margin-left: 20px;
            margin-right: 20px;
            margin-top: 10px;
        }
        .addstudent .wrap{
            margin-left: 20px;
            margin-right: 20px;
            margin-top: 20px;
        }
        .addstudent .wrap h3{
            width: 90px;
            height: 30px;
            line-height: 10px;
        }
        .addstudent .wrap .el-input{
            width: 320px;
            margin-left: 10px;
            margin-bottom: 25px;

        }
        .addstudent .wrap .row_1 .col_1{
            display: flex;
            align-items: center;
        }
        .el-table__header
        .el-table-column--selection
        .cell
        .el-checkbox:after {
            color: #333;
            content: "全选";
            font-size: 14px;
            margin-left: 5px;
            font-weight: bold;
        }
        .btnBox {
		position: absolute;
		top: 0;
		right: 0;

		.btn {
			margin-right: 20px;
		}
	}
    }   
    
        
</style>